<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>assign()</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="api.functions.html" title="Chapter 14. Smarty Class Methods">
<link rel="prev" href="api.append.by.ref.html" title="appendByRef()">
<link rel="next" href="api.assign.by.ref.html" title="assignByRef()">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">assign()</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="api.append.by.ref.html">Prev</a> </td>
<th width="60%" align="center">Chapter 14. Smarty Class Methods</th>
<td width="20%" align="right"> <a accesskey="n" href="api.assign.by.ref.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="refentry" title="assign()">
<a name="api.assign"></a><div class="titlepage"></div>
<div class="refnamediv">
<h2>Name</h2>
<p>assign() — assign variables/objects to the templates</p>
</div>
<div class="refsect1" title="Description">
<a name="id427195"></a><h2>Description</h2>
<code class="methodsynopsis"><span class="type">void </span><span class="methodname">assign</span>(<span class="methodparam"><span class="type">mixed </span><span class="parameter">var</span></span>);</code><br><code class="methodsynopsis"><span class="type">void </span><span class="methodname">assign</span>(<span class="methodparam"><span class="type">string </span><span class="parameter">varname</span></span>,<br>            <span class="methodparam"><span class="type">mixed </span><span class="parameter">var</span></span>,<br>            <span class="methodparam"><span class="type">bool </span><span class="parameter">nocache</span></span>);</code><p>
   You can  explicitly pass name/value pairs, or associative arrays
   containing the name/value pairs.
  </p>
<p>
   If you pass the optional third <em class="parameter"><code>nocache</code></em> parameter of <code class="constant">TRUE</code>, the variable is assigned as nocache variable.
   See <a class="link" href="caching.cacheable.html#cacheability.variables" title="Cacheability of Variables"><em class="parameter"><code>Cacheability of Variables</code></em></a> for details.
  </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
    When you assign/register objects to templates, be sure that all properties and methods accessed from the template are for presentation purposes only. It is very easy to inject application logic through objects, and this leads to poor designs that are difficult to manage. See the Best Practices section of the Smarty website.
  </p>
</div>
<div class="example">
<a name="id427767"></a><p class="title"><b>Example 14.6. assign()</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php
// passing name/value pairs
$smarty-&gt;assign('Name', 'Fred');
$smarty-&gt;assign('Address', $address);

// passing an associative array
$smarty-&gt;assign(array('city' =&gt; 'Lincoln', 'state' =&gt; 'Nebraska'));

// passing an array
$myArray = array('no' =&gt; 10, 'label' =&gt; 'Peanuts');
$smarty-&gt;assign('foo',$myArray);

// passing a row from a database (eg adodb)
$sql = 'select id, name, email from contacts where contact ='.$id;
$smarty-&gt;assign('contact', $db-&gt;getRow($sql));
?&gt;

</pre>
<p>
   These are accessed in the template with
   </p>
<pre class="programlisting">

{* note the vars are case sensitive like php *}
{$Name}
{$Address}
{$city}
{$state}

{$foo.no}, {$foo.label}
{$contact.id}, {$contact.name},{$contact.email}

</pre>
</div>
</div>
<br class="example-break"><p>
  To access more complex array assignments see
  <a class="link" href="language.function.foreach.html" title="{foreach},{foreachelse}"><code class="varname">{foreach}</code></a>
  and
  <a class="link" href="language.function.section.html" title="{section},{sectionelse}"><code class="varname">{section}</code></a>
  </p>
<p>
 See also
 <a class="link" href="api.assign.by.ref.html" title="assignByRef()"><code class="varname">assignByRef()</code></a>,
 <a class="link" href="api.get.template.vars.html" title="getTemplateVars()"><code class="varname">getTemplateVars()</code></a>,
 <a class="link" href="api.clear.assign.html" title="clearAssign()"><code class="varname">clearAssign()</code></a>,
 <a class="link" href="api.append.html" title="append()"><code class="varname">append()</code></a>
 and
 <a class="link" href="language.function.assign.html" title="{assign}"><code class="varname">{assign}</code></a>
 </p>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="api.append.by.ref.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="api.functions.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="api.assign.by.ref.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">appendByRef() </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> assignByRef()</td>
</tr>
</table>
</div>
</body>
</html>
